package com.xonm.db;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.xonm.vo.A_InfoVO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class DBHandler {
	
	private Context ctx;
	private DBHelper helper;
	private SQLiteDatabase db;//(Connection)
	private static DBHandler _instance;
	
	private DBHandler(Context ctx) {
		this.ctx = ctx;
		this.helper = new DBHelper(ctx,"xonm_01.db",1);
		this.db = helper.getWritableDatabase();
	}

	//singleton
	public static DBHandler open(Context ctx) throws SQLException{
		if(_instance==null){
			_instance=new DBHandler(ctx);
		}
		return _instance;
	}

	public void close(){
		helper.close();
	}

	public void insert(A_InfoVO a_InfoVO){
		
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put("project", a_InfoVO.getProject());
		values.put("host", a_InfoVO.getHost());
		values.put("person", a_InfoVO.getPerson());
		values.put("p_hp", a_InfoVO.getP_hp());
		values.put("a_yn", a_InfoVO.getA_yn());
		values.put("s_yn", a_InfoVO.getS_yn());
		values.put("f_yn", 1);
		values.put("regi_date", a_InfoVO.getRegi_date());
		
		db.insert("a_info", null, values);
		
	}
	
	public void update(A_InfoVO a_InfoVO){
		
		ContentValues values = new ContentValues();

		values.put("project", a_InfoVO.getProject());
		values.put("host", a_InfoVO.getHost());
		values.put("person", a_InfoVO.getPerson());
		values.put("p_hp", a_InfoVO.getP_hp());
		values.put("a_yn", a_InfoVO.getA_yn());
		values.put("s_yn", a_InfoVO.getS_yn());

		db.update("a_info", values, "_id=?", new String[]{ String.valueOf(a_InfoVO.get_id()) });
		
	}
	
	public void delete(A_InfoVO a_InfoVO){
		db.delete("a_info", "_id=?", new String[]{ String.valueOf(a_InfoVO.get_id()) });
	}
	
	public Cursor select_all(){
		String sql = "select _id, project, host, person, p_hp, a_yn, s_yn, f_yn, regi_date from a_info order by _id desc;";
		Cursor results = db.rawQuery(sql, null);
		return results;
	}
	
	public Cursor select_pk(A_InfoVO a_InfoVO){
		String sql = "select xd_idx as _id, xd_idx, xd_domain_name, xd_alam_yn, xd_favor_yn  from xping_domain where xd_idx = "+a_InfoVO.get_id()+";";
		Cursor result = db.rawQuery(sql, null);
		return result;
	}

	
}
